package at.ac.tuwien.ifs.calendit.basis.gui;


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.sql.Date;
import java.sql.Time;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;

import at.ac.tuwien.ifs.calendit.basis.helper.Constants;
import at.ac.tuwien.ifs.calendit.dao.ITermin;
import at.ac.tuwien.ifs.calendit.domain.Termin;

class JTableButtonModel extends AbstractTableModel {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static JTextField jt= new JTextField();
	private List <Termin> termintyp= null;
    private ITermin  terminDAO = null;
    private int typTermin;

	/**
	 * 
	 */
    
	private TerminTableModel terminTM=new TerminTableModel();
	private static List<Termin> terminList= new ArrayList<Termin>();
	public static JComponent[] btn ={
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
		new JLabel(),
	};

	public static Object[][] __rows= {
			{"06:00-06:30",btn[0]},
			{"06:30-07:00",btn[1]},
			{"07:00-07:30",btn[2]},
			{"07:30-08:00",btn[3]},
			{"08:00-08:30",btn[4]},
            {"08:30-09:00",btn[5]},
            {"09:00-09:30",btn[6]},
            {"09:30-10:00",btn[7]},
            {"10:00-10:30",btn[8]},
            {"10:30-11:00",btn[9]},
            {"11:00-11:30",btn[10]},
            {"11:30-12:00",btn[11]},
            {"12:00-12:30",btn[12]},
            {"12:30-13:00",btn[13]},
            {"13:00-13:30",btn[14]},
            {"13:30-14:00",btn[15]},
            {"14:00-14:30",btn[16]},
            {"14:30-15:00",btn[17]},
            {"15:00-15:30",btn[18]},
            {"15:30-16:00",btn[19]},
            {"16:00-16:30",btn[20]},
            {"16:30-17:00",btn[21]},
            {"17:00-17:30",btn[22]},
            {"17:30-18:00",btn[23]},
            {"18:00-18:30",btn[24]},
            {"18:30-19:00",btn[25]},
            {"19:00-19:30",btn[26]},
            {"19:30-20:00",btn[27]},
            {"20:00-20:30",btn[28]},
            {"20:30-21:00",btn[29]},
            {"21:00-21:30",btn[30]},
            {"21:30-22:00",btn[31]},
            {"22:00-22:30",btn[32]},
            {"22:30-23:00",btn[33]},
            {"23:00-23:30",btn[34]},
            {"23:30-00:00",btn[35]}
		    };
	private Date date= Constants.selectedDate;
	private void initButtons(){
		clearRows();
		
		Termin tempT= new Termin();
		
		/**
		 * Get all events to according times
		 * 36: number of rows
		 */
		int times=6;
		int i=0;
		terminList.clear();
		while(i<36){
			tempT=initTerminTM1(times);
			if(tempT==null){
				tempT=new Termin();
				tempT.setBeginnDatum(date);
				tempT.setEndDatum(date);
				tempT.setBeginnUhrzeit(new Time(times,00,00));
				tempT.setEndUhrzeit(new Time(times,30,00));
				terminList.add(tempT);
			}
			if(tempT.getBezeichnung()!=null){
				terminList.add(tempT);
				if(Tagesansicht.privat_chb.isSelected()){
					if(Tagesansicht.arbeits_chb.isSelected()){//beide sind selektiert
						btn[i]=new JButton(tempT.getBezeichnung());
						btn[i].setToolTipText(tempT.getBeschreibung());
						if(tempT.getTermintyp()==1){
							if(tempT.getMeetingtermin())
								btn[i].setBackground(Color.yellow);
							else
								btn[i].setBackground(Color.red);
						}
						else
							btn[i].setBackground(Color.blue);
						
						__rows[i][1]=btn[i];

					}
					else{//nur Privat ist selected
						if(tempT.getTermintyp()==2){
							btn[i]=new JButton(tempT.getBezeichnung());
							btn[i].setToolTipText(tempT.getBeschreibung());
							btn[i].setBackground(Color.blue);
							
							__rows[i][1]=btn[i];
						}
						else{
							
							btn[i]=new JLabel();
							btn[i].setToolTipText("");
							__rows[i][1]=btn[i];
						}
					}
				}
				else
					if(Tagesansicht.arbeits_chb.isSelected()){//nur Arbeit ist selected
						if(tempT.getTermintyp()==1){
							btn[i]=new JButton(tempT.getBezeichnung());
							btn[i].setToolTipText(tempT.getBeschreibung());
							if(tempT.getMeetingtermin()){
							btn[i].setBackground(Color.yellow);
							}
							else
								btn[i].setBackground(Color.red);
							
							__rows[i][1]=btn[i];
						}
						else{
							
							btn[i]=new JLabel();
							btn[i].setToolTipText("");
							__rows[i][1]=btn[i];
						}
					
				}
				else{//keines ist selected
					i=36; //Abbruchbedingung true setzen
				}
			}
			i++;
			tempT=initTerminTM2(times);
			if(tempT==null){
				tempT=new Termin();
				tempT.setBeginnDatum(date);
				tempT.setEndDatum(date);
				tempT.setBeginnUhrzeit(new Time(times,30,00));
				tempT.setEndUhrzeit(new Time(times+1,00,00));
				terminList.add(tempT);
			}
			if(tempT.getBezeichnung()!=null){
				terminList.add(tempT);
				if(Tagesansicht.privat_chb.isSelected()){
					if(Tagesansicht.arbeits_chb.isSelected()){//beides ist selektiert
						if(tempT.getTermintyp()==2 || tempT.getTermintyp()==1){
							btn[i]=new JButton(tempT.getBezeichnung());
							btn[i].setToolTipText(tempT.getBeschreibung());
							if(tempT.getTermintyp()==1){
								if(tempT.getMeetingtermin())
									btn[i].setBackground(Color.yellow);
								else
									btn[i].setBackground(Color.red);
							}
							else
								btn[i].setBackground(Color.blue);
							

							__rows[i][1]=btn[i];
						}
					}
					else{//nur Privat ist selected
						if(tempT.getTermintyp()==2){
							btn[i]=new JButton(tempT.getBezeichnung());
							btn[i].setToolTipText(tempT.getBeschreibung());
							btn[i].setBackground(Color.blue);
							
							__rows[i][1]=btn[i];
						}
						else{
							
							btn[i]=new JLabel();
							btn[i].setToolTipText("");
							__rows[i][1]=btn[i];
						}
					}
				}
				else
					if(Tagesansicht.arbeits_chb.isSelected()){//nur Arbeit ist selected
					if(tempT.getTermintyp()==1){
						btn[i]=new JButton(tempT.getBezeichnung());
						btn[i].setToolTipText(tempT.getBeschreibung());
						if(tempT.getMeetingtermin()){
						btn[i].setBackground(Color.yellow);
						}
						else
							btn[i].setBackground(Color.red);
						
						__rows[i][1]=btn[i];
					}
					else{
						
						btn[i]=new JLabel();
						btn[i].setToolTipText("");
						__rows[i][1]=btn[i];
					}
					
				}
				else{//keines ist selected
					i=36; //Abbruchbedingung true setzen
				}
			}
				else{
				
					btn[i]=new JLabel();
					btn[i].setToolTipText("");
					__rows[i][1]=btn[i];
				}
		i++;
		times++;
	}
}
				
	private void initGUI() {
		jt.addActionListener(new TextFieldListener());
		jt.getDocument().addDocumentListener(new DListener());
		GregorianCalendar now=new GregorianCalendar();
		DateFormat df=DateFormat.getDateInstance();
		
//		df=DateFormat.getTimeInstance(DateFormat.SHORT);
//		System.out.println(df.format(now.getTime()));
//		
//		SimpleDateFormat formatter = new SimpleDateFormat ("20yy-01-dd");

//		System.out.println("Zeit und Datum : " + formatter.format(date));

		List termine= terminTM.getTerminDao().getAll();
		Termin tempT= new Termin();
		
		
		initButtons();
	}
	public JTableButtonModel() {
		initGUI();
	}

	  private String[] __columns = { "Uhrzeit", "Event" };

	  public String getColumnName(int column) { 
	    return __columns[column];
	  }

	  public int getRowCount() {
	    return __rows.length;
	  }

	  public int getColumnCount() {
	    return __columns.length;
	  }

	  public Object getValueAt(int row, int column) { 
	      return __rows[row][column];
	  }

	  public boolean isCellEditable(int row, int column) {
	    return false;
	  }

	  public Class<? extends Object> getColumnClass(int column) {
	    return getValueAt(0, column).getClass();
	  }
	  
	  public Termin initTerminTM1(int time){
		Termin t= new Termin();
		Time sendingtime1= new Time(time,29,00);
		t.setBeginnDatum((java.sql.Date) date);
		t.setBeginnUhrzeit(sendingtime1);
		Time sendingtime= new Time(time,00,00);
		t.setEndUhrzeit(sendingtime);
		t.setMitarbeiterID(Constants.getUserId());
		terminTM.getTerminToTermintyp(time);
		
//		System.out.println("Select * from Termin where BeginnDatum="+  t.getBeginnDatum()+" and beginnuhrzeit &lt;="+ sendingtime1+
//				 "and enduhrzeit &gt;="+sendingtime+" and mitarbeiterid=?");
		return terminTM.getPropToTerminTime(t);
	  }
	  
	  public Termin initTerminTM2(int time){
			Termin t= new Termin();
			Time sendingtime1= new Time(time,59,00);
			t.setBeginnDatum((java.sql.Date) date);
			t.setBeginnUhrzeit(sendingtime1);
			Time sendingtime= new Time(time,30,00);
			t.setEndUhrzeit(sendingtime);
			t.setMitarbeiterID(Constants.getUserId());
//			System.out.println("Select * from Termin where BeginnDatum="+ t.getBeginnDatum()+" and beginnuhrzeit &lt;="+ sendingtime1+
//					 "and enduhrzeit &gt;="+sendingtime+" and mitarbeiterid=?");
			return terminTM.getPropToTerminTime(t);
		  }
	  
	  private void calComponentResized(ComponentEvent evt) {
		  //TODO add your code for cal.mousePressed
		  date=Constants.selectedDate;
		  initButtons();
		 
	  }
	  private class TextFieldListener implements ActionListener{

			public void actionPerformed(ActionEvent e) {
				  //TODO add your code for cal.mousePressed
				  date=Constants.selectedDate;
				  initButtons();
				
			}
	    	
	    }
	  
	  private class DListener implements DocumentListener{


			public void changedUpdate(DocumentEvent arg0) {
				  //TODO add your code for cal.mousePressed
				  date=Constants.selectedDate;
				  initButtons();
				
			}

			public void insertUpdate(DocumentEvent arg0) {
				  //TODO add your code for cal.mousePressed
				  date=Constants.selectedDate;
				  initButtons();
				
			}

			public void removeUpdate(DocumentEvent arg0) {

				  date=Constants.selectedDate;
				  initButtons();
							}
	  }
	  public void clearRows(){
		 int i=0;
		  while(i<36){
			  __rows[i][1]=new JLabel();
			  i++;
		  }
	  }
	  
	  public static Termin getTerminAt(int row){
//		  for(int i=0;i<terminList.size();i++){
//			  System.out.println("tt"+terminList.get(i).getBeschreibung());
//		  }
		  return terminList.get(row);
	  }
	}